package org.alex.nettychat.netty.first;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import org.alex.nettychat.netty.first.handler.FirstServerHandler;

/**
 * Netty 服务端
 * @author : alex
 */
public class NettyServer {

    public static void main(String[] args) {
        ServerBootstrap serverBootstrap = new ServerBootstrap();

        /**
         * 1.boss对应IOServer.java中的负责接收新连接的线程，主要负责创建新连接。
         * 2.worker对应IOServer.java中的负责读取数据的线程，主要用于读取数据及业务逻辑处理。
         */
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup();

        serverBootstrap.group(boss, worker)
                .channel(NioServerSocketChannel.class)
                // 数据处理逻辑
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel channel) throws Exception {
                       channel.pipeline().addLast(new FirstServerHandler());

                    }
                });
        bind(serverBootstrap, 8000);

    }

    private static void bind(final ServerBootstrap serverBootstrap, final int port) {
        serverBootstrap.bind(8000).addListener(new GenericFutureListener<Future<? super Void>>() {
            @Override
            public void operationComplete(Future<? super Void> future) throws Exception {
                if (future.isSuccess()) {
                    System.out.println("端口:[" + port +"]绑定成功");
                } else {
                    System.out.println("端口:[" + port +"]绑定失败");
                    bind(serverBootstrap, port + 1);
                }
            }
        });
    }
}
